<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-tickets"></i>公共抽奖列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!-- <el-button type="primary" icon="delete" class="handle-del mr10" @click="delAll">批量删除</el-button> -->
                <el-date-picker v-model="startTime" type="datetime" class="handle-select" placeholder="开始日期" ></el-date-picker>
                <el-date-picker v-model="endTime" type="datetime"  class="handle-select" placeholder="结束日期"></el-date-picker>
                <el-select v-model="seachPutStatus" placeholder="上架状态" class="handle-select mr10">
                    <el-option key="-1" label="全部" value="-1"></el-option>
                    <el-option key="1" label="上架" value="1"></el-option>
                    <el-option key="0" label="下架" value="0"></el-option>
                    <el-option key="2" label="自动上架" value="2"></el-option>
                </el-select>
                <el-input v-model="prizeProvider" placeholder="赞助商" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="search" @click="search">搜索</el-button>
                <el-button type="primary" icon="el-icon-circle-plus-outline" class="add-user" @click="addSystemUser">添加考拉抽奖</el-button>
            </div>
            <el-table :data="luckDrawData" border style="width: 100%" ref="multipleTable" @selection-change="handleSelectionChange">
                <!-- <el-table-column type="selection" width="40"></el-table-column> -->
                <el-table-column prop="id" label="抽奖编号" sortable align="center">
                </el-table-column>
                <el-table-column prop="prizeProvider" label="赞助商" align="center">
                </el-table-column>
                <el-table-column prop="prizeName" label="抽奖商品"  align="center">
                    <template slot-scope="scope">
                         <div v-for='prize in scope.row.prizes' :key="prize.id">{{prize.prizeName}}</div>
                    </template>
                </el-table-column>
                <el-table-column prop="count" label="数量" align="center">
                     <template slot-scope="scope">
                         <div v-for='prize in scope.row.prizes' :key="prize.id">{{prize.count}}</div>
                    </template>
                </el-table-column>
                <el-table-column prop="isForward" label="小程序路径" align="center" :formatter="isForward">
                </el-table-column>
                <el-table-column prop="drawTime" label="实际开奖时间" align="center">
                </el-table-column>
                <el-table-column prop="setLotteryTime" label="设置开奖时间" align="center">
                </el-table-column>
                <el-table-column prop="createTime" label="创建时间" align="center">
                </el-table-column>
                <el-table-column prop="joinNum" label="参与人数" align="center">
                </el-table-column>
                <el-table-column prop="status" label="开奖状态" align="center" :formatter="status">
                </el-table-column>
                <el-table-column prop="putStatus" label="上架状态" align="center" :formatter="putStatus">
                </el-table-column>
                <el-table-column label="操作" width="180" fixed="right" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" @click="see(scope.$index, scope.row)">查看</el-button>
                        <el-button size="mini" type="primary" v-if="scope.row.status == 1" @click="edit(scope.$index, scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage4"
                    :page-sizes="[10, 15, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="50px">
                <el-form-item label="日期">
                    <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
                </el-form-item>
                <el-form-item label="姓名">
                    <el-input ></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input ></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { Message } from 'element-ui';
import { getLuckDrawData,searchLuckDrawData } from '../../components/api/LuckDraw.js';
    export default {
        // inject:['reload'],
        name: 'basetable',
        data() {
            return {
                tableData: [],
                multipleSelection: [],
                seachPutStatus: '',
                prizeProvider: '',
                pageSize:10,
                pageNum:1,
                del_list: [],
                is_search: false,
                editVisible: false,
                delVisible: false,
                form: {
                    name: '',
                    date: '',
                    address: ''
                },
                systemForm:{
                    userName:'',
                    phone:'',
                    password:'',
                    status:''
                },
                 systemRules:{
                    userName: [
                        { required:true, message:'请填写用户名', trigger: 'blur' }
                    ],
                    phone: [
                        { required:true, message:'请填写手机号', trigger: 'blur' }
                    ],
                    password: [
                        { required:true, message:'请填写密码' , trigger: 'blur' }
                    ],
                    status:[
                        { required:true, message:'请填写状态', trigger:'blur'}
                    ]
                },
                luckDrawData:[],
                startTime:'',
                endTime:'',

                dialogImageUrl: '',
                dialogVisible: false,
                idx: -1,
                currentPage4:1,
                total:0
            }
        },
        created() {
            this.getData();
            // this.reload();
        },
        activated(){
            this.getData();
        },
        computed: {
            // data() {
            //     return this.tableData.filter((d) => {
            //         let is_del = false;
            //         console.log('============');
            //         console.log(this.del_list);
            //         for (let i = 0; i < this.del_list.length; i++) {
            //             if (d.name === this.del_list[i].name) {
            //                 is_del = true;
            //                 break;
            //             }
            //         }
            //         if (!is_del) {
            //             if (d.address.indexOf(this.select_cate) > -1 &&
            //                 (d.name.indexOf(this.select_word) > -1 ||
            //                     d.address.indexOf(this.select_word) > -1)
            //             ) {
            //                 return d;
            //             }
            //         }
            //     })
            // }
        },
        methods: {

            //选择列表数据条数
            handleSizeChange(val) {
                console.log('每页'+val+'条数');
                this.pageSize = val;
                this.getData();
            },
            //选择列表数据分页
            handleCurrentChange(val) {
                console.log('当前页'+val);
                this.pageNum = val;
                this.getData();
            },

            getData() {
                let router = this.$router;
                let params = {
                    startDate:this.startTime,
                    endDate:this.endTime,
                    prizeProvider:this.prizeProvider,
                    type:1,
                    pageNum:this.pageNum,
                    putStatus:this.seachPutStatus,
                    pageSize:this.pageSize
                };

                //获取列表数据
               getLuckDrawData(params).then((res)=>{
                   if(res.status == 200){
                       this.luckDrawData = res.data.list;
                       this.total = res.data.total
                   }
                   console.log(res);
               }).catch((err)=>{
                   console.log(err);
               });
            },

            //列表搜索功能
            search() {
                this.getData();
            },

            isForward(row, column) {
                if(row.isForward == 0){
                    return '无'
                }else if(row.isForward == 1){
                    return '有'
                }
            },

            status(row,column){
                if(row.status == 1){
                    return '未开奖'
                }else if(row.status == 2){
                    return '已开奖'
                }else{
                    return '失效'
                }
            },
             putStatus(row,column){
                if(row.putStatus == 1){
                    return '上架'
                }else if(row.putStatus == 2){
                    return '自动上架'
                }else{
                    return '下架'
                }
            },
            prizeName(row,column){
                let prize = row.prizes;
                for(let i=0,len = prize.length;i<len;i++){
                    
                    return prize[i].parzeName;
                }
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            handleEdit(index, row) {
                this.idx = index;
                const item = this.tableData[index];
                this.form = {
                    name: item.name,
                    date: item.date,
                    address: item.address
                }
                this.editVisible = true;
            },

            //查看活动详情
            see(index, row) {
                // this.idx = index;
                // this.delVisible = true;
                // this.delId = row.id;
                // console.log(this.delId);
                console.log(index);
                console.log(row.id);
                this.$router.push({name:'luckDrawDetails',params:{id:row.id}});
                
            },
            //编辑活动
            edit(index,row){
                console.log(row.id);
                
                this.$router.push({name:'LuckDrawEdit',params:{id:row.id}});
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 保存编辑
            saveEdit() {
                this.$set(this.tableData, this.idx, this.form);
                this.editVisible = false;
                this.$message.success(`修改第 ${this.idx+1} 行成功`);
            },
            // 确定删除
            deleteRow(){
              
                
            },
            //添加抽奖
            addSystemUser(){
                this.$router.push('/addLuckDraw');
            },
           

            //重置表单
            resetForm(formName){
                this.$refs[formName].resetFields();
            },
        }
    }

</script>

<style scoped>
    .add-user{
        float:right;
    }
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 190px;
    }

    .handle-input {
        width: 190px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .handle-botton{
        margin: 0 0 20px 80px;
    }
</style>
